package com.syaccp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.syaccp.model.SysMenu;
import com.syaccp.request.SysMenuRequest;
import com.syaccp.request.SysMenuSearchRequest;
import com.syaccp.service.SysMenuService;
import com.syaccp.utils.Result;
import com.syaccp.vo.MenuCheckVo;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 系统菜单管理
 * @author Administrator
 *
 */

@RestController
@RequestMapping("api/admin/system/menu")
@Api(tags = "系统菜单管理")
public class SysMenuController {

	 @Autowired
	 private SysMenuService sysMenuService;


	 /**
	     * 菜单列表
	     * @param request 搜索条件
	     */
	    //@PreAuthorize("hasAuthority('admin:system:menu:list')")
	    @ApiOperation(value = "菜单列表")
	    @RequestMapping(value = "/list", method = RequestMethod.GET)
	    public Result<List<SysMenu>> getList(SysMenuSearchRequest request) {
	        return Result.success(sysMenuService.getAdminList(request));
	    }

	    /**
	     * 新增菜单
	     * @param systemMenuRequest 新增菜单
	     */
	    //@PreAuthorize("hasAuthority('admin:system:menu:add')")
	    @ApiOperation(value = "新增菜单")
	    @RequestMapping(value = "/add", method = RequestMethod.POST)
	    public Result<String> add(@RequestBody @Validated SysMenuRequest sysMenuRequest) {
	        if (sysMenuService.add(sysMenuRequest)) {
	            return Result.success();
	        }
	        return Result.failed();
	    }

	    /**
	     * 删除菜单
	     * @param id Integer
	     */
	   // @PreAuthorize("hasAuthority('admin:system:menu:delete')")
	    @ApiOperation(value = "删除菜单")
	    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	    public Result<String> delete(@PathVariable(value = "id") Integer id) {
	        if (sysMenuService.deleteById(id)) {
	            return Result.success();
	        }
	        return Result.failed();
	    }

	    /**
	     * 修改菜单
	     */
	   // @PreAuthorize("hasAuthority('admin:system:menu:update')")
	    @ApiOperation(value = "修改菜单")
	    @RequestMapping(value = "/update", method = RequestMethod.POST)
	    public Result<String> update(@RequestBody @Validated SysMenuRequest sysMenuRequest) {
	        if (sysMenuService.edit(sysMenuRequest)) {
	            return Result.success();
	        }
	        return Result.failed();
	    }

	    /**
	     * 菜单详情
	     */
	    //@PreAuthorize("hasAuthority('admin:system:menu:info')")
	    @ApiOperation(value = "菜单详情")
	    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
	    public Result<SysMenu> info(@PathVariable(value = "id") Integer id) {
	        return Result.success(sysMenuService.getInfo(id));
	   }

	    /**
	     * 修改菜单显示状态
	     */
	    @PreAuthorize("hasAuthority('admin:system:menu:show:status')")
	    @ApiOperation(value = "修改菜单显示状态")
	    @RequestMapping(value = "/updateShowStatus/{id}", method = RequestMethod.POST)
	    public Result<String> updateShowStatus(@PathVariable(value = "id") Integer id) {
	        if (sysMenuService.updateShowStatus(id)) {
	            return Result.success("修改成功");
	        }
	        return Result.failed("修改失败");
	    }

	    /**
	     * 菜单缓存树
	     */
	    @PreAuthorize("hasAuthority('admin:system:menu:cache:tree')")
	    @ApiOperation(value = "菜单缓存树")
	    @RequestMapping(value = "/cache/tree", method = RequestMethod.GET)
	    public Result<List<MenuCheckVo>> getCacheTree() {
	        return Result.success(sysMenuService.getCacheTree());
	    }

}
